package org.esprit.ocm.client.ui.one;

import java.util.List;

import org.esprit.ocm.client.model.oca.datasource.HostLocalDS;
import org.esprit.ocm.client.rpc.oca.HostOcaService;
import org.esprit.ocm.client.rpc.oca.HostOcaServiceAsync;
import org.esprit.ocm.client.rpc.oca.VMOcaService;
import org.esprit.ocm.client.rpc.oca.VMOcaServiceAsync;
import org.esprit.ocm.dto.impl.AwsCredentialsDto;
import org.esprit.ocm.dto.impl.ServerDto;
import org.esprit.ocm.dto.impl.UserDto;
import org.esprit.ocm.model.oca.HostOca;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGridField;

public class ManageVM extends Window {

	private final VMOcaServiceAsync		vmOcaService	= GWT.create(VMOcaService.class);
	private final HostOcaServiceAsync	hostOcaService	= GWT.create(HostOcaService.class);

	private DataSource					ds;

	private SelectItem					selectItem;

	public ManageVM(final String order, final int idVM, final ServerDto server, final AwsCredentialsDto credentials, final UserDto user) {

		setTitle("Manage VM");
		setWidth(300);
		setHeight(150);
		setIsModal(true);
		setShowModalMask(true);
		setShowCloseButton(true);
		centerInPage();

		final DynamicForm form = new DynamicForm();
		form.setWidth(250);
		form.setHeight(20);
		form.setPadding(5);
		form.setLayoutAlign(VerticalAlignment.TOP);

		selectItem = new SelectItem("Hosts");


		String secret = credentials.getAwsAccessKeyId() + ":" + credentials.getSecretAccessKey();
		String endpoint = server.getIpServer();
		
		hostOcaService.listAll(secret, endpoint, new AsyncCallback<List<HostOca>>() {

			public void onSuccess(List<HostOca> list) {
				// TODO Auto-generated method stub
				for (HostOca hostOca : list) {
					System.out.println(hostOca.getId());
				}

				ds = HostLocalDS.getInstance(list);

				ListGridField idField = new ListGridField("id");
				ListGridField name = new ListGridField("name");

				selectItem.setOptionDataSource(ds);
				selectItem.setDefaultToFirstOption(true);
				selectItem.setWidth(240);
				selectItem.setTitle("Hosts");
				selectItem.setValueField("id");
				selectItem.setDisplayField("name");
				selectItem.setPickListWidth(450);
				selectItem.setPickListFields(idField, name);

			}

			public void onFailure(Throwable arg0) {
				// TODO Auto-generated method stub

			}
		});

		ButtonItem createButton = new ButtonItem("create", "Create");
		createButton.setAlign(Alignment.CENTER);
		createButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				destroy();
			}
		});

		form.setFields(selectItem, createButton);
		form.redraw();

		addItem(form);
		draw();

	}

}
